package pers.cnzdy.tutorial.Chapter9_Perception_Media.Section2_Location;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import pers.cnzdy.tutorial.Chapter9_Perception_Media.Section5_Camera.CameraActivity;
import pers.cnzdy.tutorial.R;

public class LocationActivity extends AppCompatActivity {
    private TextView locationTextView;
    private LocationManager locationManager;
    private String provider;
    private static final int REQUEST_CODE = 200;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);

        locationTextView = findViewById(R.id.location_text_view);

        if (ActivityCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION) !=
                PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this,
                        Manifest.permission.ACCESS_COARSE_LOCATION) !=
                        PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(LocationActivity.this,
                    new String[]{
                            Manifest.permission.READ_PHONE_STATE,
                            Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.ACCESS_COARSE_LOCATION
                    },
                    REQUEST_CODE);
        } else {
            position();
        }
    }

    private void position() {
        locationManager = (LocationManager) getSystemService(
                Context.LOCATION_SERVICE);

        List<String> providerList = locationManager.getProviders(true);
        if (providerList.contains(LocationManager.GPS_PROVIDER)) {
            provider = LocationManager.GPS_PROVIDER;
        } else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
            provider = LocationManager.NETWORK_PROVIDER;
        } else {
            Toast.makeText(this, "无法提供位置信息",
                    Toast.LENGTH_SHORT).show();
            return;
        }

        Location location = locationManager.getLastKnownLocation(provider);

        if (location != null) {
            updateLocation(location);
            //locationManager.requestLocationUpdates(provider, 5000, 1, locationListener);
            Toast.makeText(this, "开始定位", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "location is null", Toast.LENGTH_SHORT).show();
        }

        locationManager.requestLocationUpdates(provider,
                1000, 1, locationListener);

    }

    LocationListener locationListener = new LocationListener() {

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLocationChanged(Location location) {
            updateLocation(location);
        }
    };

    private void updateLocation(Location location) {
        if (location == null) {
            Toast.makeText(this, "location is null", Toast.LENGTH_SHORT).show();
        }

        String currentPosition = "GPS 定位：" + "\n"
                + "        经度： " + location.getLongitude() + "\n"
                + "        纬度： " + location.getLatitude();
        locationTextView.setText(currentPosition);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[],
                                           int[] grantResults) {
        if (grantResults.length > 0 && grantResults[0] ==
                PackageManager.PERMISSION_GRANTED) {
            position();
        } else { // 申请失败，可以继续向用户解释。
            Toast.makeText(LocationActivity.this, "没有权限，无法使用定位",
                    Toast.LENGTH_SHORT).show();
        }
        return;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (locationManager != null) {
            locationManager.removeUpdates(locationListener);
        }
    }
}
